package com.mission.schedule.utils;

/**
 * Created by liwei on 2017/10/10.
 */


import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class RxEncryptUtils {
    public static String DES_Transformation = "DES/ECB/NoPadding";
    private static final String DES_Algorithm = "DES";
    public static String TripleDES_Transformation = "DESede/ECB/NoPadding";
    private static final String TripleDES_Algorithm = "DESede";
    public static String AES_Transformation = "AES/ECB/NoPadding";
    private static final String AES_Algorithm = "AES";

    public RxEncryptUtils() {
    }

    public static String encryptMD2ToString(String data) {
        return encryptMD2ToString(data.getBytes());
    }

    public static String encryptMD2ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptMD2(data));
    }

    public static byte[] encryptMD2(byte[] data) {
        return encryptAlgorithm(data, "MD2");
    }

    public static String encryptMD5ToString(String data) {
        return encryptMD5ToString(data.getBytes());
    }

    public static String encryptMD5ToString(String data, String salt) {
        return RxDataUtils.bytes2HexString(encryptMD5((data + salt).getBytes()));
    }

    public static String encryptMD5ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptMD5(data));
    }

    public static String encryptMD5ToString(byte[] data, byte[] salt) {
        byte[] dataSalt = new byte[data.length + salt.length];
        System.arraycopy(data, 0, dataSalt, 0, data.length);
        System.arraycopy(salt, 0, dataSalt, data.length, salt.length);
        return RxDataUtils.bytes2HexString(encryptMD5(dataSalt));
    }

    public static byte[] encryptMD5(byte[] data) {
        return encryptAlgorithm(data, "MD5");
    }

    public static String encryptMD5File2String(String filePath) {
        return encryptMD5File2String(new File(filePath));
    }

    public static byte[] encryptMD5File(String filePath) {
        return encryptMD5File(new File(filePath));
    }

    public static String encryptMD5File2String(File file) {
        return encryptMD5File(file) != null?RxDataUtils.bytes2HexString(encryptMD5File(file)):"";
    }

    public static byte[] encryptMD5File(File file) {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);
            FileChannel e = fis.getChannel();
            MappedByteBuffer buffer = e.map(MapMode.READ_ONLY, 0L, file.length());
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(buffer);
            byte[] var5 = md.digest();
            return var5;
        } catch (IOException | NoSuchAlgorithmException var9) {
            var9.printStackTrace();
        } finally {
            RxFileUtils.closeIO(new Closeable[]{fis});
        }

        return null;
    }

    public static String encryptSHA1ToString(String data) {
        return encryptSHA1ToString(data.getBytes());
    }

    public static String encryptSHA1ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptSHA1(data));
    }

    public static byte[] encryptSHA1(byte[] data) {
        return encryptAlgorithm(data, "SHA-1");
    }

    public static String encryptSHA224ToString(String data) {
        return encryptSHA224ToString(data.getBytes());
    }

    public static String encryptSHA224ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptSHA224(data));
    }

    public static byte[] encryptSHA224(byte[] data) {
        return encryptAlgorithm(data, "SHA-224");
    }

    public static String encryptSHA256ToString(String data) {
        return encryptSHA256ToString(data.getBytes());
    }

    public static String encryptSHA256ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptSHA256(data));
    }

    public static byte[] encryptSHA256(byte[] data) {
        return encryptAlgorithm(data, "SHA-256");
    }

    public static String encryptSHA384ToString(String data) {
        return encryptSHA384ToString(data.getBytes());
    }

    public static String encryptSHA384ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptSHA384(data));
    }

    public static byte[] encryptSHA384(byte[] data) {
        return encryptAlgorithm(data, "SHA-384");
    }

    public static String encryptSHA512ToString(String data) {
        return encryptSHA512ToString(data.getBytes());
    }

    public static String encryptSHA512ToString(byte[] data) {
        return RxDataUtils.bytes2HexString(encryptSHA512(data));
    }

    public static byte[] encryptSHA512(byte[] data) {
        return encryptAlgorithm(data, "SHA-512");
    }

    private static byte[] encryptAlgorithm(byte[] data, String algorithm) {
        try {
            MessageDigest e = MessageDigest.getInstance(algorithm);
            e.update(data);
            return e.digest();
        } catch (NoSuchAlgorithmException var3) {
            var3.printStackTrace();
            return new byte[0];
        }
    }

    public static byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) {
        try {
            SecretKeySpec e = new SecretKeySpec(key, algorithm);
            Cipher cipher = Cipher.getInstance(transformation);
            SecureRandom random = new SecureRandom();
            cipher.init(isEncrypt?1:2, e, random);
            return cipher.doFinal(data);
        } catch (Throwable var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public static byte[] encryptDES2Base64(byte[] data, byte[] key) {
        return RxEncodeUtils.base64Encode(encryptDES(data, key));
    }

    public static String encryptDES2HexString(byte[] data, byte[] key) {
        return RxDataUtils.bytes2HexString(encryptDES(data, key));
    }

    public static byte[] encryptDES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "DES", DES_Transformation, true);
    }

    public static byte[] decryptBase64DES(byte[] data, byte[] key) {
        return decryptDES(RxEncodeUtils.base64Decode(data), key);
    }

    public static byte[] decryptHexStringDES(String data, byte[] key) {
        return decryptDES(RxDataUtils.hexString2Bytes(data), key);
    }

    public static byte[] decryptDES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "DES", DES_Transformation, false);
    }

    public static byte[] encrypt3DES2Base64(byte[] data, byte[] key) {
        return RxEncodeUtils.base64Encode(encrypt3DES(data, key));
    }

    public static String encrypt3DES2HexString(byte[] data, byte[] key) {
        return RxDataUtils.bytes2HexString(encrypt3DES(data, key));
    }

    public static byte[] encrypt3DES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "DESede", TripleDES_Transformation, true);
    }

    public static byte[] decryptBase64_3DES(byte[] data, byte[] key) {
        return decrypt3DES(RxEncodeUtils.base64Decode(data), key);
    }

    public static byte[] decryptHexString3DES(String data, byte[] key) {
        return decrypt3DES(RxDataUtils.hexString2Bytes(data), key);
    }

    public static byte[] decrypt3DES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "DESede", TripleDES_Transformation, false);
    }

    public static byte[] encryptAES2Base64(byte[] data, byte[] key) {
        return RxEncodeUtils.base64Encode(encryptAES(data, key));
    }

    public static String encryptAES2HexString(byte[] data, byte[] key) {
        return RxDataUtils.bytes2HexString(encryptAES(data, key));
    }

    public static byte[] encryptAES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "AES", AES_Transformation, true);
    }

    public static byte[] decryptBase64AES(byte[] data, byte[] key) {
        return decryptAES(RxEncodeUtils.base64Decode(data), key);
    }

    public static byte[] decryptHexStringAES(String data, byte[] key) {
        return decryptAES(RxDataUtils.hexString2Bytes(data), key);
    }

    public static byte[] decryptAES(byte[] data, byte[] key) {
        return DESTemplet(data, key, "AES", AES_Transformation, false);
    }
}